$(document).ready(function(){
	$("#password").on("keypress", function(e){
		if(e.which == 13){
			$("#loginBtn").trigger("click");
		}
	});
	
	$("#loginBtn").on("click", function(){
		if(!validFiled()) return;

		var frmObj = $("#frmLogin");
		var chkEnc = true;//Encoding Option
		var iterCnt = 500;//Encoding Option
		var uname = $("#username").val();
		var pwd = $(frmObj).find("input[type=password]").val();
		
		if(chkEnc){
			var encPwd = $(frmObj).find("input[name=j_password]");
			if($(encPwd).length > 0) $(encPwd).remove();

			uname += ";"+chkEnc+";"+iterCnt;
			pwd = b64aes128(pwd, iterCnt);
		}

		$(frmObj).append("<input type='hidden' name='j_username' value='"+uname+"' />");
		$(frmObj).append("<input type='hidden' name='j_password' value='"+pwd+"' />");		
		$(frmObj).submit();
	});

});

function validFiled(){
	var def = new validDefinition();
	var validator = new formValidator("frmLogin", def.rules, def.messages);
	return validator.validation();
}

function b64aes128(plainText, iterCnt){
	var iv = "00000000000000000000000000000000";
	var salt = "00000000000000000000000000000000";
	var keySize = 128;
	var iterationCount = iterCnt;	
	var passPhrase = "aesalgoisbestbes";
	var aes = new CryptoJsAes(keySize, iterationCount);
	var encrypt = aes.encrypt(salt, iv, passPhrase, plainText);
	
	return encrypt;
}